package com.asa.tongji;

import java.util.Arrays;

public class TUtils2 {
/*********************置信区间什么的8*****************************/
	
	
	
	
	/**
	 * 点估计，合适的样本比例置信区间
	 * 
	 * @param x	样本总数
	 * @param xx	合适样本数量
	 * @return
	 */

	public static double[] zhixingqujian(int x, int xx) {
		double p = (double) xx / (double) x;// 这个可以做期望
		double biaozhuncha = Math.sqrt(p * (1 - p) / x);// 这个呢可以做方程
		double[] asa = new double[3];
		asa[0] = p - 2 * biaozhuncha;
		asa[1] = p;
		asa[2] = p + 2 * biaozhuncha;
		return asa;
	}

	/**
	 * 直接根据样本空间来确定样本均值置信区间
	 * @param x 样本空间
	 * @param a	置信度
	 * @return
	 */
	public static double[] zongtijunzhizhixingqujian2(double[] x, double a) {
		double xx = TUtils1.average(x);
		double ss = TUtils1.variance(x);
		a = FenBuFunction.zhengtai(a);
		double e = a * ss / Math.sqrt(x.length + 1);
		double[] asa = new double[2];
		asa[0] = xx - e;
		asa[1] = xx + e;

		return asa;
	}
	
	public static void main(String[] args) {
		
		double[] x = {1,2,3,4,5,6,7,8,9};
		double[] asa = zongtijunzhizhixingqujian2(x , 0.80);
		//System.out.println(Arrays.toString(asa));
		
	}
	
	
	
	
	
	
	
	
	/**
	 * 总体样本均值的置信区间，方差已知
	 * @param n	样本数量
	 * @param oo	标准差
	 * @param u	样本均值
	 * @param a	置信度
	 * @return
	 */
	public static double[] zongtijunzhi(int n,double oo,double u,double a){
		a = FenBuFunction.zhengtai(a);
		double e = a*oo/Math.sqrt(n);
		//System.out.println(e);
		double[] asa = new double[2];
		asa[0] = u - e;
		asa[1] = u + e;
		return asa;
	}
	/**
	 * 总体样本均值的置信区间，方差未知
	 * @param n	样本数量
	 * @param s	标准差
	 * @param u	样本均值
	 * @param t	t分布，样本置信度
	 * @return
	 * 	public static void main(String[] args) throws Exception {
		double[] zongtijunzhi2 = zongtijunzhi2(23, 7.2, 47, 0.95);
		for (int i = 0; i < zongtijunzhi2.length; i++) {
			//System.out.println(zongtijunzhi2[i]);
		}
	}
	 */


	public static double[] zongtijunzhi2(int n,double s,double u,double t){
		t = FenBuFunction.tfengbu(t, n);
		double e = t*s/Math.sqrt((double)n);
		double[] asa = new double[2];
		asa[0] = u - e;
		asa[1] = u + e;
		return asa;
	}

	
	


	
	
	/**
	 * 总体方差的估计
	 * @param a 置信度
	 * @param n	样本大小
	 * @param o	样本方差
	 * @return	标准差区间
	 * @throws Exception 样本n和置信度a数据的合法性检验
	 */

	public static double[] zongtifangcha(double a,int n,double o) throws Exception{
		double[] kafangfengbu = FenBuFunction.kafangfengbu(a, n);
		double[] asa = new double[2];
		asa[0] = Math.sqrt((n-1)*o/kafangfengbu[1]);
		asa[1] = Math.sqrt((n-1)*o/kafangfengbu[0]);
		return asa;
	}
	
	
	
	/**
	 * 均值单侧区间估计
	 * @param u	总体均值
	 * @param a	置信度
	 * @param n	样本数量
	 * @param o	样本标准差
	 * @return	取值范围
	 */
	public static double junzhidangche(double u,double a,int n,double o){
		double tfengbu = FenBuFunction.tfengbu(a, n-1);
		//System.out.println(tfengbu);
		tfengbu = 2.1318;
		return u-Math.sqrt(o/n)*tfengbu;
	}
	
//	public static void main(String[] args) throws Exception {
//		double junzhidangche = junzhidangche(1160, 0.9, 5, 9950);
//		//System.out.println(junzhidangche);
//	}
//	
	
	
	
	
	/**
	 * 根据置信区间，确定需要的样本大小，这个或多或少需要前面的统计数据支持，来对现有的进行评估 90% 95% 99%对应的标准误差1.65 1.96
	 * 2.58
	 * 
	 * @param p
	 *            概率,如果此概率不正常(未知概率填写大于1小于0都可以的)，则默认会取0.5
	 * @param a
	 *            置信度
	 * @param e
	 *            允许的误差
	 * @return 达到要求需要的样本数
	 */

	public static double yangbendaxiao(double p, double a, double e) {
		a = FenBuFunction.zhengtai(a);
		if (p > 0 && p < 1)
			return a * a * p * (1 - p) / (e * e);
		else {
			return a * a * 0.25 / (e * e);
		}
	}


	
}
